#include <stdio.h>
#include <stdlib.h>

#include "link_queue.h"

LinkQueue* create_lq(void)
{
    LinkQueue *queue = malloc(sizeof(LinkQueue));
    queue->front = NULL;
    queue->rear = NULL;
    return queue;
}

bool empty_lq(LinkQueue* queue)
{
    return NULL == queue->front;
}

bool pop_lq(LinkQueue* queue)
{
    if(empty_lq(queue))
    {
        return false;
    }
    Node *tmp = queue->front;
    queue->front = tmp->next;
    free(tmp);
    return true;
}

void push_lq(LinkQueue* queue, void* data)
{
    Node *tmp = create_n(data);
    if(empty_lq(queue))
    {
        queue->front = tmp;
        queue->rear = tmp;
    }
    else
    {
        queue->rear->next = tmp;
        queue->rear = tmp;
    }
}

void destory_lq(LinkQueue* queue)
{
    while(pop_lq(queue));
    free(queue);
}

void* front_lq(LinkQueue* queue)
{
    return queue->front->data;
}

void* rear_lq(LinkQueue* queue)
{
    return queue->rear->data;
}

size_t size_lq(LinkQueue* queue)
{
    size_t size = 0;
    for(Node *tmp = queue->front; NULL != tmp; tmp = tmp->next,size++);
    return size;
}
